<?php
include_once('class/class.user.php');

$user = new user();

$users = array();




$users = $user->users();

if(isset($_GET['ubah_user'])){
	//session_start();
    $kodespa=$_GET['id']; 
    $ubah=$user->cekUser($kodespa);
	//var_dump($dataubah);
	}

if(isset($_POST['login']))
	{
		//session_start();
		$spa=$_POST['spa'];
		$password=$_POST['password'];
		
		if(empty($spa) || empty($password))
		{
			$error_login="Username dan Password tidak boleh kosong";
			session_destroy();
		}
		else
		{
			$cek=$user->login($spa,$password);
			//var_dump ($cek);
			if($cek==1)
				{
					$sesi = $user->cekUser($spa);
					//var_dump ($user->cekUser($spa));
					foreach($sesi AS $row){
					$_SESSION['login']=$row['kd_user']; //code yang bermasalah
					$_SESSION['spa']=$row[1];
					$_SESSION['roles']=$row[3];
					//header("location: utama.php");
					//echo "login berhasil";
					
				} 
			
		}	else {
					echo "Username atau Password salah";
					//session_destroy();
				}
		
	}
	}
	
if(isset($_POST['Rekam_user']))
	{
		$spa=$_POST['spa'];
		//$username=$_POST['username'];
		$password=$_POST['Password'];		
		$pass=$_POST['pass'];
		$email=$_POST['email'];
		$roles=$_POST['Roles'];
		
		if(empty($spa) || empty($password) || empty($email) || empty($roles))
		{
			echo "Field username, password, email dan kewenangan wajib diisi!!";
		}
		elseif($password <> $pass)
		{
			echo "Password tidak sama";
		}
		else
		{
			$cek=$user->rekamUser($spa, $password, $email, $roles);
			if($cek==1)
				{
					echo "user terekam";
				} else {
					echo "user gagal terekam";
				}
		}	
		
	}
	
if(isset($_POST['ubah_user']))
	{
		$spa=$_POST['spa'];
		//$username=$_POST['username'];
		$password=$_POST['Password'];		
		$pass=$_POST['pass'];
		$email=$_POST['email'];
		$roles=$_POST['Roles'];
		
		if(empty($password) || empty($email) || empty($roles))
		{
			echo "Field username, password, email dan kewenangan wajib diisi!!";
		}
		elseif($password <> $pass)
		{
			echo "Password tidak sama";
		}
		else
		{
			$cek=$user->editUser($spa, $password, $email, $roles);
			var_dump($cek);
			if($cek==1)
				{
					echo "user terekam";
				} else {
					echo "user gagal terekam";
				}
		}	
		
	}
	
	

?>